package com.xiran.srpingboottemplateself.controller;

import com.xiran.srpingboottemplateself.common.Response;
import com.xiran.srpingboottemplateself.pojo.entity.Blog;
import com.xiran.srpingboottemplateself.pojo.entity.User;
import com.xiran.srpingboottemplateself.service.BlogService;
import com.xiran.srpingboottemplateself.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "博客模块")
@RestController
@RequestMapping("/api/blog")
@RequiredArgsConstructor
public class BlogController {

    private final BlogService blogService;
    private final UserService userService;

    @ApiOperation("公开博客列表")
    @GetMapping("/public/list")
    public Response<List<Blog>> publicBlogs() {
        List<Blog> blogs = blogService.getPublicBlogs();
        return Response.success(blogs);
    }

    @ApiOperation("博客详情")
    @GetMapping("/{id}")
    public Response<Blog> blogDetail(@PathVariable Long id, Authentication authentication) {
        // 从认证信息中获取用户名，然后查询用户ID
        String username = authentication.getName();
        Long userId = getUserIdFromUsername(username);

        Blog blog = blogService.getBlogById(id, userId);
        if (blog == null) {
            return Response.fail("博客不存在或无权访问");
        }
        return Response.success(blog);
    }

    @ApiOperation("创建博客")
    @PostMapping
    public Response<String> createBlog(@RequestBody Blog blog, Authentication authentication) {
        String username = authentication.getName();
        Long userId = getUserIdFromUsername(username);

        blog.setAuthorId(userId);
        boolean success = blogService.createBlog(blog);
        return success ? Response.success("创建成功") : Response.fail("创建失败");
    }

    @ApiOperation("获取我的博客")
    @GetMapping("/my")
    public Response<List<Blog>> myBlogs(Authentication authentication) {
        String username = authentication.getName();
        Long userId = getUserIdFromUsername(username);

        List<Blog> blogs = blogService.getUserBlogs(userId);
        return Response.success(blogs);
    }

    @ApiOperation("更新博客")
    @PutMapping("/{id}")
    public Response<String> updateBlog(@PathVariable Long id, @RequestBody Blog blog, Authentication authentication) {
        String username = authentication.getName();
        Long userId = getUserIdFromUsername(username);

        // 检查博客是否存在且属于当前用户
        Blog existingBlog = blogService.getBlogById(id, userId);
        if (existingBlog == null) {
            return Response.fail("博客不存在或无权访问");
        }

        blog.setId(id);
        blog.setAuthorId(userId); // 确保作者ID不变
        boolean success = blogService.updateBlog(blog);
        return success ? Response.success("更新成功") : Response.fail("更新失败");
    }

    @ApiOperation("删除博客")
    @DeleteMapping("/{id}")
    public Response<String> deleteBlog(@PathVariable Long id, Authentication authentication) {
        String username = authentication.getName();
        Long userId = getUserIdFromUsername(username);

        boolean success = blogService.deleteBlog(id, userId);
        return success ? Response.success("删除成功") : Response.fail("删除失败或无权限");
    }

    // 修复：根据用户名获取用户ID
    private Long getUserIdFromUsername(String username) {
        User user = userService.findByUsername(username);
        return user != null ? user.getId() : null;
    }
}